﻿Imports System.Data.OleDb
Imports System.IO
Imports Subgurim.Controles

Partial Public Class _New
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        GMap1.setCenter(New GLatLng(24.69943, 46.72348))
        Dim options As New GMapUIOptions()
        options.maptypes_physical = False
        options.zoom_scrollwheel = True
        GMap1.addGMapUI(New GMapUI())
        GMap1.GZoom = 10
    End Sub
    Protected Function GMap1_Click(ByVal s As Object, ByVal e As GAjaxServerEventArgs) As String
        Return New GMarker(e.point).ToString(e.map)

    End Function

    Protected Function GMap1_MarkerClick(ByVal s As Object, ByVal e As GAjaxServerEventArgs) As String
        Dim markerID As String = GMap1.getGMapElementById(e.who)

        Dim js As String = String.Format(vbCr & vbLf & "if ({0}.isHidden())" & vbCr & vbLf & " {0}.show()" & vbCr & vbLf & " else" & vbCr & vbLf & " {0}.hide();" & vbCr & vbLf & "  ", markerID)

        Dim Address = markerID
   
        js += String.Format("javascript:AddAddress( {0}.getPoint());", Address)

        Return js
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)


        Dim RestaurantsInsert As String = "INSERT INTO Resturants(RestName, Address, Telephone, Email, PriceLevel, LuxuryLevel, EatingService,WorkingHours,UserName) VALUES (@RestName,@Latitude,@Telephone,@Email,@PriceLevel,@LuxuryLevel,@EatingService,@WorkingHours,@UserName)"
        Dim FacilitatsInsert As String = "INSERT INTO Facilities(Restid,Internet,Parking,Delivery,Toilet,ChildChair,TV,Music,PlayingArea) VALUES(@Restid,@Internet,@Parking,@Delivery,@Toilet,@ChildChair,@TV,@Music,@PlayingArea)"

        Dim SQLcmd As OleDbCommand = New OleDbCommand(RestaurantsInsert, OleDbConn)
        Dim SQL2cmd As OleDbCommand = New OleDbCommand(FacilitatsInsert, OleDbConn)


        SQLcmd.CommandType = CommandType.Text
        SQL2cmd.CommandType = CommandType.Text


        'Resturants table
        SQLcmd.Parameters.AddWithValue("@RestName", RestNametxt.Text)
        SQLcmd.Parameters.AddWithValue("@Address", Addresstxt.Text)
        SQLcmd.Parameters.AddWithValue("@Telephone", Telephonetxt.Text)
        SQLcmd.Parameters.AddWithValue("@Email", Emailtxt.Text)
        SQLcmd.Parameters.AddWithValue("@PriceLevel", DropDownList1.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@LuxuryLevel", DropDownList2.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@EatingService", DropDownList3.SelectedItem.Text)
        SQLcmd.Parameters.AddWithValue("@WorkingHours", Hourstxt.Text)
        SQLcmd.Parameters.AddWithValue("@UserName", User.Identity.Name)

        OleDbConn.Open()

        'Find-out final resturant ID from DB
        Dim LatestID As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY", OleDbConn)
        LatestID.CommandType = CommandType.Text

        SQLcmd.ExecuteNonQuery()

        'Final resturant ID from DB and saved in fID
        Dim fID As Integer = LatestID.ExecuteScalar()
        Session("RID") = fID

        'Facilities table
        SQL2cmd.Parameters.AddWithValue("@Restid", fID)

        If CheckBox11.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Internet", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Internet", "0")
        End If

        If CheckBox12.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Parking", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Parking", "0")
        End If
        If CheckBox13.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Delivery", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Delivery", "0")
        End If
        If CheckBox14.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Toilet", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Toilet", "0")
        End If
        If CheckBox15.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@ChildChair", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@ChildChair", "0")
        End If
        If CheckBox16.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@TV", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@TV", "0")
        End If
        If CheckBox17.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@Music", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@Music", "0")
        End If
        If CheckBox18.Checked = True Then
            SQL2cmd.Parameters.AddWithValue("@PlayingArea", "1")
        Else
            SQL2cmd.Parameters.AddWithValue("@PlayingArea", "0")
        End If

        SQL2cmd.ExecuteNonQuery()



        'Insert Rest_Food_Type table
        For Each itms As ListItem In ListBox1.Items

            If itms.Selected = True Then
                Dim RestFoodTypeInsert As String = "INSERT INTO Rest_Food_Type(RestId,FoodTypeId) VALUES(@RestId,@FoodTypeId)"
                Dim SQL3cmd As OleDbCommand = New OleDbCommand(RestFoodTypeInsert, OleDbConn)
                SQL3cmd.CommandType = CommandType.Text

                SQL3cmd.Parameters.Clear()
                SQL3cmd.Parameters.AddWithValue("@Restid", fID)
                SQL3cmd.Parameters.AddWithValue("@FoodTypeId", itms.Value)
                SQL3cmd.ExecuteNonQuery()
            End If

        Next


        'Upload images Rest_Images table.
        If (FileUpload1.HasFile = True) And (FileUpload1.FileBytes.Length <= 845941) Then

            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload1.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload1.FileName)
            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload1.FileName)
            SQLimg.ExecuteNonQuery()

        Else

            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text
            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", "restaurant.jpg")
            SQLimg.ExecuteNonQuery()

        End If


        If (FileUpload2.HasFile = True) And (FileUpload2.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload2.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload2.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload2.FileName)
            SQLimg.ExecuteNonQuery()
        Else
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", "restaurant.jpg")
            SQLimg.ExecuteNonQuery()

        End If

        If (FileUpload3.HasFile = True) And (FileUpload3.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload3.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload3.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload3.FileName)
            SQLimg.ExecuteNonQuery()
        Else
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", "restaurant.jpg")
            SQLimg.ExecuteNonQuery()

        End If

        If (FileUpload4.HasFile = True) And (FileUpload4.FileBytes.Length <= 845941) Then
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            Me.FileUpload4.SaveAs(Server.MapPath("~/RestImages/") & fID & "_" & FileUpload4.FileName)

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", fID & "_" & FileUpload4.FileName)
            SQLimg.ExecuteNonQuery()
        Else
            Dim ResImagesInsert As String = "INSERT INTO Rest_Images(RestId,ImgName) VALUES(@RestId,@ImgName)"
            Dim SQLimg As OleDbCommand = New OleDbCommand(ResImagesInsert, OleDbConn)
            SQLimg.CommandType = CommandType.Text

            SQLimg.Parameters.AddWithValue("@RestId", fID)
            SQLimg.Parameters.AddWithValue("@ImgName", "restaurant.jpg")
            SQLimg.ExecuteNonQuery()

        End If
        


        OleDbConn.Close()

        Response.Redirect("Details.aspx?Id=" & fID)

    End Sub


End Class